﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace WJP.Logging
{
    /// <summary>
    /// Console日志记录器，输出日志到标准输出流
    /// </summary>
    [Serializable]
    public class ConsoleLogger : AbstractLogger
    {
        public ConsoleLogger() : this(string.Empty, LogLevel.Debug)
        {

        }

        public ConsoleLogger(LogLevel level) : this(string.Empty, level)
        {

        }

        public ConsoleLogger(string name) : this(name, LogLevel.Debug)
        {

        }

        public ConsoleLogger(string name, LogLevel level) : base(name, level)
        {

        }

        public override ILogger CreateChildLogger(string loggerName)
        {
            if (loggerName.IsNullOrEmpty())
                throw new ArgumentNullException(nameof(loggerName), "子日志记录器的名称不可为空");

            return new ConsoleLogger("{0}.{1}".FormatArgs(Name, loggerName), Level);
        }

        protected override void Log(LogLevel level, string loggerName, string message, Exception exception)
        {
            Console.Out.WriteLine("[{0}] '{1}' {2}", level, loggerName, message);

            if (exception != null)
            {
                Console.Out.WriteLine("[{0}] '{1}' {2}: {3} {4}", level, loggerName, exception.GetType().FullName,
                                      exception.Message, exception.StackTrace);
            }
        }
    }
}
